package com.yixia.camera.util;

import java.io.File;
import java.io.FileOutputStream;

public class FileUtils {

    public static boolean deleteFile(File f) {
        if (f != null && f.exists() && !f.isDirectory()) {
            return f.delete();
        }
        return false;
    }

    public static void deleteDir(File f) {
        if (f != null && f.exists() && f.isDirectory()) {
            for (File file : f.listFiles()) {
                if (file.isDirectory()) deleteDir(file);
                file.delete();
            }
            f.delete();
        }
    }

    public static void deleteDir(String f) {
        if (f != null && f.length() > 0) {
            deleteDir(new File(f));
        }
    }

    public static boolean deleteFile(String f) {
        if (f != null && f.length() > 0) {
            return deleteFile(new File(f));
        }
        return false;
    }

    /**
     * 保存临时文件
     */
    public static String saveFile(byte[] bytes) {
        try {
            File file = File.createTempFile("img", ".jpg");
//            File file = new File("/data/data/com.chinawlx.wlxfamily/cache/", "img" + SystemClock
//                    .currentThreadTimeMillis() / 1000);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.flush();//没有flush不代表它就没有输出出，只是可能没有完全输出。调用flush是保证缓存清空输出
            fos.close();

            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        } return "";
    }
}
